package com.minglead.crawler.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Maps;
import com.minglead.crawler.base.ActionResponse;
import com.minglead.crawler.base.ActionResult;
import com.minglead.crawler.constant.YqCrawlerConstant;
import com.minglead.crawler.entity.SelectEntity;
import com.minglead.crawler.entity.YqBasicInfoDetailEntity;
import com.minglead.crawler.entity.YqBasicInfoEntity;
import com.minglead.crawler.entity.YqCrawlerEntity;
import com.minglead.crawler.enums.CrawlerGlobalKeyYesOrNoEnum;
import com.minglead.crawler.enums.CrawlerStateEnum;
import com.minglead.crawler.feign.PriceFeignClient;
import com.minglead.crawler.filter.YqCrawlerFilter;
import com.minglead.crawler.service.YqCrawlerService;
import com.minglead.crawler.util.FileUtils;
import com.minglead.crawler.util.PageUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description: 舆情爬虫配置表controller
 * &#064;author:  bxh
 * @date: 2024-8-1
 **/

@Slf4j
@RestController
//@ModuleNameMonitor(name = "舆情配置")
@Tag(name = "舆情爬虫配置YQ_CRAWLER", description = "舆情爬虫配置表controller")
@RequestMapping("/yq/yqCrawler")
public class YqCrawlerController {

    @Autowired
    private YqCrawlerService yqCrawlerService;

    @Autowired
    private PriceFeignClient priceFeignClient;

    @Operation(summary = "新增爬虫媒体")
    @PostMapping("/test")
    public ActionResult test() {
        return ActionResult.success("成功", priceFeignClient.listCrawlerState());
    }

    @Operation(summary = "新增爬虫媒体")
    @PostMapping("/saveCrawlerMedia")
    public ActionResult saveCrawlerMedia(@RequestBody YqCrawlerEntity entity) {
        return ActionResult.success("成功", yqCrawlerService.saveCrawlerMedia(entity));
    }

    @Operation(summary = "根据id获取表单")
    @GetMapping("/getById/{id}")
    public ActionResult getById(@PathVariable String id) throws Exception {
        return ActionResult.success("成功", yqCrawlerService.getById(id));
    }

    @Operation(summary = "获取分页")
    @PostMapping("/listPage")
    public ActionResult<IPage<YqCrawlerEntity>> listPage(@RequestBody YqCrawlerFilter filter) throws Exception {
        // 分页查询
        IPage<YqCrawlerEntity> iPage = yqCrawlerService.listPage(filter);
        return PageUtils.convertPage(iPage);
    }

    @Operation(summary = "保存")
    @PostMapping("/update")
    public ActionResult update(@RequestBody YqCrawlerEntity entity) {
        ActionResponse r = yqCrawlerService.updateKeyword(entity);
        return ActionResult.success(r.getMessage(), r.getData());
    }

    @Operation(summary = "启动爬虫")
    @PostMapping("/startCrawlerById/{id}")
    public ActionResult startCrawlerById(@PathVariable String id) {
        ActionResponse r = yqCrawlerService.startCrawlerById(id);
        return ActionResult.success(r.getMessage(), r.getData());
    }

    @Operation(summary = "停止爬虫")
    @PostMapping("/stopCrawlerById/{id}")
    public ActionResult stopCrawlerById(@PathVariable String id) {
        ActionResponse r = yqCrawlerService.stopCrawlerById(id);
        return ActionResult.success(r.getMessage(), r.getData());
    }

    @Operation(summary = "获取日志")
    @GetMapping("/getLogById/{id}")
    public ActionResult getLogById(@PathVariable String id) {
        ActionResponse r = yqCrawlerService.getLogById(id);
        return ActionResult.success(r.getMessage(), r.getData());
    }

    @Operation(summary = "获取全局关键词")
    @GetMapping("/getGlobalKeyword")
    public ActionResult getGlobalKeyword() {
        File globalFile = new File(YqCrawlerConstant.CRAWLER_GLOBAL_KEYWORD);
        return ActionResult.success(FileUtils.getFileContent(globalFile));
    }

    @Operation(summary = "更新全局关键词")
    @PostMapping("/updateGlobalKeyword")
    public ActionResult updateGlobalKeyword(@RequestBody YqCrawlerEntity entity) {
        ActionResponse r = yqCrawlerService.updateGlobalKeyword(entity.getKeyWord());
        return ActionResult.success(r.getMessage(), r.getData());
    }

    @Operation(summary = "获取文章基础信息")
    @GetMapping("/getBasicInfo")
    public ActionResult getBasicInfo(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String date) {
        return ActionResult.success("成功", yqCrawlerService.getBasicInfo(date));
    }

    @Operation(summary = "获取文章内容详情")
    @GetMapping("/getBasicInfoDetail")
    public ActionResult getBasicInfoDetail(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String date) {
        return ActionResult.success("成功", yqCrawlerService.getBasicInfoDetail(date));
    }

    @Operation(summary = "获取广东省统计局爬虫结果数据")
    @GetMapping("/getDataMacroIndicatorData")
    public ActionResult getDataMacroIndicatorData(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String date) {
        return ActionResult.success("成功", yqCrawlerService.getDataMacroIndicatorData(date));
    }

    @Operation(summary = "获取广东省统计局爬虫日志")
    @GetMapping("/getDataBigmercGrabLog")
    public ActionResult getDataBigmercGrabLog(@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String date) {
        return ActionResult.success("成功", yqCrawlerService.getDataBigmercGrabLog(date));
    }

    @Operation(summary = "舆情爬虫配置预准备数据")
    @GetMapping("/preparedData")
    public ActionResult preparedData() {
        Map<String, List<SelectEntity>> resultMap = Maps.newHashMap();
        // 状态
        resultMap.put("stateList", CrawlerStateEnum.getSelectList());
        // 是否采用全局关键词
        resultMap.put("globalKeyYesOrNoList", CrawlerGlobalKeyYesOrNoEnum.getSelectList());
        return ActionResult.success(resultMap);
    }

}
